package no.nordicsemi.android.toolbox.scanner

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothSocket
import android.os.ParcelUuid
import androidx.hilt.navigation.compose.hiltViewModel
import no.nordicsemi.android.common.navigation.createDestination
import no.nordicsemi.android.common.navigation.defineDestination
import no.nordicsemi.android.common.navigation.viewmodel.SimpleNavigationViewModel
import no.nordicsemi.android.kotlin.ble.core.RealServerDevice
import no.nordicsemi.android.kotlin.ble.core.ServerDevice
import no.nordicsemi.android.kotlin.ble.ui.scanner.DeviceSelected
import no.nordicsemi.android.kotlin.ble.ui.scanner.ScannerScreen
import no.nordicsemi.android.kotlin.ble.ui.scanner.ScanningCancelled
import java.io.IOException
import java.util.UUID
import kotlin.concurrent.thread


val ScannerDestinationId = createDestination<ParcelUuid, ServerDevice>("uiscanner-destination")
val ScannerDestination1 = defineDestination(ScannerDestinationId) {
    val navigationViewModel = hiltViewModel<SimpleNavigationViewModel>()

    val arg = navigationViewModel.parameterOf(ScannerDestinationId)

    ScannerScreen(
        uuid = arg,
        onResult = {
            when (it) {
                is DeviceSelected -> navigationViewModel.navigateUpWithResult(ScannerDestinationId, it.scanResults.device)
                ScanningCancelled -> navigationViewModel.navigateUp()
            }
        }
    )
}

val ScannerDestination = defineDestination(ScannerDestinationId) {
    val navigationViewModel = hiltViewModel<SimpleNavigationViewModel>()

    val arg = navigationViewModel.parameterOf(ScannerDestinationId)


    ScannerScreen(

        uuid = arg,
        onResult = {
            lateinit var mBluetoothAdapter: BluetoothAdapter
            var mBluetoothSocket: BluetoothSocket? = null
            var isBlueConnected: Boolean = false
            var blueAddress: String = "DC:4F:F5:D5:8A:0C"//""CA:9A:DA:AC:E1:FB"//"DF:2A:9E:B6:79:40"         //no use
            var blueName: String ="EEG"
            var eegUUID: UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
            thread{
                try {
                    val bluetoothdevice: BluetoothDevice
                    //这一段代码必须在子线程处理，直接使用协程会阻塞主线程，所以用Thread,其实也可以直接用Thread，不用协程
                    if ( mBluetoothSocket== null || !isBlueConnected) {
                        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
                        bluetoothdevice = mBluetoothAdapter.getRemoteDevice(blueAddress)

                        var device: RealServerDevice = RealServerDevice(bluetoothdevice)

                        //val device = IntentCompat.getParcelableExtra(intent!!, DEVICE_DATA, ServerDevice::class.java)!!
                        println("####################ScannerScreen device: $device")
                        navigationViewModel.navigateUpWithResult(ScannerDestinationId, device)

                    }
                } catch (e: IOException) {
                    //连接失败销毁Activity
                    println("***************error")
                    e.printStackTrace()
                }

            }
//            when (it) {
//                is DeviceSelected -> navigationViewModel.navigateUpWithResult(ScannerDestinationId, it.scanResults.device)
//                ScanningCancelled -> navigationViewModel.navigateUp()
//            }
            navigationViewModel.navigateUp()
            println("===============--------------================$it.scanResults.device")
            println("===============--------------================$(it).scanResults.device")
            println("===============--------------================$(it.scanResults.device)")
        }
    )
}
